package com.gimi.cloud.jimi.manage.controller.sys;

import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.reqhead.Response;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.dto.sys.SysRoleDTO;
import com.gimi.cloud.jimi.dto.params.ModifySysRoleParam;
import com.gimi.cloud.jimi.dto.params.RoleListParams;
import com.gimi.cloud.jimi.dto.params.RoleSingleParams;
import com.gimi.cloud.jimi.manage.service.sys.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by shazhiwei on 2017/12/15.
 */
@RestController
@RequestMapping(value = "role")
public class RoleController {
    private final RoleService roleService;

    @Autowired
    public RoleController(RoleService roleService){
        this.roleService = roleService;
    }

    /** 获取所有角色列表 */
    @RequestMapping(value = "/list")
    public void list(@RequestBody String params, HttpServletResponse response) {
        Response resp = new Response();
        List<SysRoleDTO> list = roleService.getRoleList();
        resp.setRes_data(list);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(resp), response);
    }

    /** 查询角色分页列表 */
    @RequestMapping(value = "/pageList")
    public void pageList(@RequestBody RoleListParams params, HttpServletResponse response) {
        Response resp = new Response();
        PageInfo<SysRoleDTO> pageInfo = roleService.getRolePageList(params);
        resp.setRes_data(pageInfo);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(resp), response);
    }

    /** 查询角色分页列表 */
    @RequestMapping(value = "/single")
    public void single(@RequestBody RoleSingleParams params, HttpServletResponse response) {
        Response resp = new Response();
        SysRoleDTO data = roleService.getRoleSingle(params);
        resp.setRes_data(data);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    /** 新增编辑角色 */
    @RequestMapping(value = "/add")
    public void addRole(@RequestBody ModifySysRoleParam params, HttpServletResponse response) {
        Response resp = new Response();
        ModifySysRoleParam result = roleService.addRole(params);
        ResponseCodeEnum code = result.getResponseCodeEnum();
        if(code != null){
            resp.setResponseCode(code);
        }else{
            resp.setResponseCode(ResponseCodeEnum.UNKNOWN_ERROR);
        }
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    @RequestMapping(value = "/edit")
    public void editRole(@RequestBody ModifySysRoleParam params, HttpServletResponse response) {
        Response resp = new Response();
        ModifySysRoleParam result = roleService.editRole(params);
        ResponseCodeEnum code = result.getResponseCodeEnum();
        if(code != null){
            resp.setResponseCode(code);
        }else{
            resp.setResponseCode(ResponseCodeEnum.UNKNOWN_ERROR);
        }
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }


}
